package com.songdehuai.supermarket.presenter.imp;

import android.app.Activity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.songdehuai.supermarket.config.Apis;
import com.songdehuai.supermarket.presenter.MainPresenter;
import com.songdehuai.supermarket.result.Result;
import com.songdehuai.supermarket.supermarket.entity.Shop;
import com.songdehuai.supermarket.sync.MainSync;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class MainPresenterImp implements MainPresenter {

    private Activity activity;
    private MainSync sync;


    public MainPresenterImp(Activity activity, MainSync sync) {
        this.activity = activity;
        this.sync = sync;
    }

    @Override
    public void getShopList(final boolean isLoadMore) {
        RequestParams requestParams = new RequestParams(Apis.SHOP);
        x.http().get(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                Type type = new TypeToken<Result<List<Shop>>>() {
                }.getType();
                Result<List<Shop>> listResult = gson.fromJson(result, type);
                if (listResult.getCode() == 0) {
                    List<Shop> shopList = listResult.getResult();
                    sync.showShopList(isLoadMore, shopList);
                } else {
                    sync.showTost("错误");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ex.printStackTrace();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


}
